<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:nwisdocs="http://wales.nhs.uk/nwis/docs"
	exclude-result-prefixes="xs xsi">
	
	<xsl:template name="medications" match="nwisdocs:Medications">
		<div class="boxed-section" id="{local-name()}">
			<h2>Medications</h2>

      <xsl:variable name="shareWithGP" select="nwisdocs:IsShareableWithGP = 'true'" />
      <xsl:variable name="isSensitive" select="nwisdocs:IsSensitive = 'true'" />
      <xsl:variable name="showMeds" select="($shareWithGP = true()) or (($shareWithGP = false()) and ($isSensitive=false()))" />
      <xsl:choose>
          <xsl:when test="$showMeds = false()">
              <div class="inline-content">
                <span class="inline-content">No medications have been recorded</span>
              </div>
          </xsl:when>
          <xsl:when test="nwisdocs:MedsCount = 0">
            <div class="inline-content">
              <xsl:value-of select="nwisdocs:MedicationAdditionalInformation/nwisdocs:Narrative"/>
            </div>
          </xsl:when>
          <xsl:otherwise>
            <table>
              <thead>
                <tr>
                  <th colspan="8">Medications at the time of discharge</th>
                </tr>
                <tr>
                  <td style="width:5%"/>
                  <td style="width:50%">Medication name</td>
                  <td>Dose Changed</td>
                  <td>Status</td>
                  <td>GP to continue</td>
                  <td>Duration</td>
                  <td>Start date</td>
                  <td>Quantity Supplied</td>
                </tr>
              </thead>
              <tbody>
                <xsl:variable name="count" select="nwisdocs:ToTakeMedications/@count" />
                <xsl:for-each select="nwisdocs:ToTakeMedications/nwisdocs:ContinuingMedication">
                  <tr>
                    <td>
                      <xsl:value-of select="@position"/> of <xsl:value-of select="$count"/>
                    </td>
                    <td>
                      <xsl:value-of select="nwisdocs:Medication/nwisdocs:DrugName/nwisdocs:DrugDescription"/> - <xsl:value-of select="nwisdocs:Medication/nwisdocs:StructuredDosage/nwisdocs:Route"/>
                      <br/><xsl:value-of select="nwisdocs:Medication/nwisdocs:StructuredDosage/nwisdocs:Dose/nwisdocs:DoseDescription"/>
                    </td>
                    <td>
                      <xsl:choose>
                        <xsl:when test="nwisdocs:DoseChanged = 'true'">
                          <xsl:choose>
                            <xsl:when test="nwisdocs:Origin = 1">
                              Yes
                            </xsl:when>
                          </xsl:choose>
                        </xsl:when>
                      </xsl:choose>
                    </td>
                    <td>
                      <xsl:value-of select="nwisdocs:MedicationAction/nwisdocs:Text"/>
                    </td>
                    <td>
                      <!--<xsl:value-of select="wcp:FormatBool(nwisdocs:GPrepeat, 'Yes', 'No')"/>-->
                    </td>
                    <td>
                      <xsl:value-of select="nwisdocs:DurationSupplied/nwisdocs:Text"/>
                    </td>
                    <td>
                      <xsl:value-of select="nwisdocs:StartDate/nwisdocs:Text"/>
                    </td>
                    <td>
                      <xsl:value-of select="nwisdocs:QuantitySupplied/nwisdocs:Text"/>
                    </td>
                  </tr>
                </xsl:for-each>
              </tbody>
            </table>
            <xsl:if test="nwisdocs:StoppedMedications/@count &gt; 0">
            <table>
              <thead>
                <tr>
                  <th colspan="3">Medications stopped during admission</th>
                </tr>
                <tr>
                  <td style="width:5%"/>
                  <td style="width:50%">Medication name</td>
                  <td>Reason</td>
                </tr>
              </thead>
              <tbody>
                <xsl:variable name="count" select="nwisdocs:StoppedMedications/@count" />
                <xsl:for-each select="nwisdocs:StoppedMedications/nwisdocs:StoppedMedication">
                  <tr>
                    <td>
                      <xsl:value-of select="@position"/> of <xsl:value-of select="$count"/>
                    </td>
                    <td>
                      <xsl:value-of select="nwisdocs:Medication/nwisdocs:DrugName/nwisdocs:DrugDescription"/> - <xsl:value-of select="nwisdocs:Route"/>
                      <br/> DOSE - <xsl:value-of select="nwisdocs:DoseAndDirections"/>
                    </td>
                    <td>
                      <xsl:value-of select="nwisdocs:ReasonStopped"/>
                    </td>
                  </tr>
                </xsl:for-each>
              </tbody>
            </table>
            </xsl:if>
            <table>
              <thead>
                <tr>
                  <th>Additional medication information</th>
                </tr>
              </thead>
              <tbody>
                <tr class="additionalmedicationinformation">
                  <td>
                     <xsl:value-of select="nwisdocs:AdditionalMedicationInformationText/nwisdocs:Narrative"/>
                  </td>
                </tr>
              </tbody>
            </table>

            <div>
              <span class="prescribedbylabel">Prescribed by:</span>
              <xsl:value-of select="nwisdocs:PrescribedBy/nwisdocs:PersonName/nwisdocs:KnownAs"/>
              <span class="contactdetailslabel">Contact No:</span>
              <xsl:value-of select="nwisdocs:PrescribedBy/nwisdocs:ContactInformation/nwisdocs:Data"/>
            </div>


            <xsl:choose>
              <xsl:when test="nwisdocs:ToTakeMedications/nwisdocs:PharmacyCheck/nwisdocs:CheckedBy[text() != 'Not checked by pharmacist']">
            <table>
              <thead>
                <tr>
                  <th>Pharmacist check</th>
                </tr>
              </thead>
              <tbody>
                <tr class="clinicalcheck" >
                  <td>
                    <span class="pharmacistchecklabel">Pharmacist check by:</span>
                    <span>
                      <xsl:variable name="checkedby" select="nwisdocs:ToTakeMedications/nwisdocs:PharmacyCheck/nwisdocs:CheckedBy"/>
                      <xsl:value-of select="$checkedby"/>
                      <xsl:choose>
                        <xsl:when test="nwisdocs:ToTakeMedications/nwisdocs:PharmacyCheck/nwisdocs:CheckedDate=''"/>
                        <xsl:otherwise>
                          on <xsl:value-of select="nwisdocs:ToTakeMedications/nwisdocs:PharmacyCheck/nwisdocs:CheckedDate"/>
                        </xsl:otherwise>
                      </xsl:choose>
                    </span>
                  </td>
                </tr>
              </tbody>
            </table>
              </xsl:when>
            </xsl:choose>
            
          </xsl:otherwise>
      </xsl:choose>
		</div>
	</xsl:template>
</xsl:stylesheet>
